# -*- coding: utf-8 -*-
"""
Created on Mon Aug 15 11:19:04 2022

@author: JK
"""
import cv2
import  numpy as np
import os
from pylab import * 



#均值哈希算法
def aHash(img):
    #缩放为8*8
    img=cv2.resize(img,(8,8),interpolation=cv2.INTER_CUBIC)
    #转换为灰度图
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #s为像素和初值为0，hash_str为hash值初值为''
    s=0
    hash_str=''
    #遍历累加求像素和
    for i in range(8):
        for j in range(8):
            s=s+gray[i,j]
    #求平均灰度
    avg=s/64
    #灰度大于平均值为1相反为0生成图片的hash值
    for i in range(8):
        for j in range(8):
            if  gray[i,j]>avg:
                hash_str=hash_str+'1'
            else:
                hash_str=hash_str+'0'            
    return hash_str

#差值感知算法
def dHash(img):
    #缩放8*8
    img=cv2.resize(img,(9,8),interpolation=cv2.INTER_CUBIC)
    #转换灰度图
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    hash_str=''
    #每行前一个像素大于后一个像素为1，相反为0，生成哈希
    for i in range(8):
        for j in range(8):
            if   gray[i,j]>gray[i,j+1]:
                hash_str=hash_str+'1'
            else:
                hash_str=hash_str+'0'
    return hash_str

#Hash值对比
def cmpHash(hash1,hash2):
    n=0
    #hash长度不同则返回-1代表传参出错
    if len(hash1)!=len(hash2):
        return -1
    #遍历判断
    for i in range(len(hash1)):
        #不相等则n计数+1，n最终为相似度
        if hash1[i]!=hash2[i]:
            n=n+1
    return n

def read_path(file_pathname):
    # 遍历该目录下的所有图片文件
    list1 = []
    list2 = []
    
    filename = os.listdir(file_pathname)
    for i, v in enumerate(filename):
        if (i == 199):
            break
        # print(filename)
        else:
            img1 = cv2.imread(file_pathname+'/'+v)
            img2 = cv2.imread(file_pathname+'/'+filename[i+1])



# img1=cv2.imread(r'D:\JC2021\IVUS menkong\test1\PDNB26PB_0002.jpg')
# img2=cv2.imread(r'D:\JC2021\IVUS menkong\test1\PDNB26PB_0003.jpg')
            hash1= aHash(img1)
            hash2= aHash(img2)
            print(hash1)
            print(hash2)
            n1=cmpHash(hash1,hash2)
            list1.append(n1)
            print('均值哈希算法相似度：',list1)
            
            
            hash1= dHash(img1)
            hash2= dHash(img2)
            print(hash1)
            print(hash2)
            n2=cmpHash(hash1,hash2)
            list2.append(n2)
             
            
            print('差值哈希算法相似度：',list2)
            x = range(len(list1))
            plt.plot(x,list1)




read_path(r"D:\JC2021\IVUS menkong\test_one")
